/*****************************************************************************
 * audio.c: JNI native audio functions for VLC Java Bindings
 *****************************************************************************
 * Copyright (C) 1998-2006 The VideoLAN team
 * Copyright (C) 2007 The LegendTV Project
 *
 * Authors: Filippo Carone <filippo@carone.org>
 *          Philippe Morin <phmorin@free.fr>
 * 			Guy Paddock <gap7472@rit.edu>
 * 
 * $Id $
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include <jni.h>
#include <vlc/libvlc.h>

#include "../faults/jFaults.h"	// JNI fault tolerance

#include "generated/Audio.h"	// JVLC internal imports, generated by gcjh

#include "instance.h"
#include "exceptions.h"
#include "input.h"

JNIEXPORT jint JNICALL Java_com_googlecode_legendtv_core_Audio_getTrack(JNIEnv *env, jobject _this)
{
	jint	retVal	= 0;
	
	JFAULT_TOLERANT_SCOPE_WITH_RETURN(env,
	{
		libvlc_exception_t*	exception	= JVLC_initException();
		libvlc_instance_t*	instance	= JVLC_getIntfInstance(env, _this);
		libvlc_input_t*		input;
		
		input	= JVLC_getInputThread(env, instance, exception);
		retVal	= libvlc_audio_get_track(input, exception);
	    
	    CHECK_EXCEPTION_FREE;
	}, retVal);
	
	return retVal;
}

JNIEXPORT void JNICALL Java_com_googlecode_legendtv_core_Audio_setTrack(JNIEnv *env, jobject _this, jint value)
{
	JFAULT_TOLERANT_SCOPE(env,
	{
		libvlc_exception_t*	exception	= JVLC_initException();
		libvlc_instance_t*	instance	= JVLC_getIntfInstance(env, _this);
		libvlc_input_t*		input;
		
	    input	= JVLC_getInputThread(env, instance, exception);
	
	    libvlc_audio_set_track(input, value, exception);
	
	    CHECK_EXCEPTION_FREE;
	});
}

JNIEXPORT jint JNICALL Java_com_googlecode_legendtv_core_Audio_getChannelNumber(JNIEnv *env, jobject _this)
{
	jint	retVal = 0;
	
	JFAULT_TOLERANT_SCOPE_WITH_RETURN(env,
	{
		libvlc_exception_t*	exception	= JVLC_initException();
		libvlc_instance_t*	instance	= JVLC_getIntfInstance(env, _this);

		retVal	= libvlc_audio_get_channel(instance, exception);
	
	    CHECK_EXCEPTION_FREE;
	}, retVal);
	
	return retVal;
}

JNIEXPORT void JNICALL Java_com_googlecode_legendtv_core_Audio_setChannelNumber(JNIEnv *env, jobject _this, jint channel)
{
	JFAULT_TOLERANT_SCOPE(env,
	{
	    libvlc_exception_t*	exception	= JVLC_initException();
		libvlc_instance_t*	instance	= JVLC_getIntfInstance(env, _this);
		
		libvlc_audio_set_channel(instance, channel, exception);
	
	    CHECK_EXCEPTION_FREE;
	});
}

JNIEXPORT jboolean JNICALL Java_com_googlecode_legendtv_core_Audio_isMuted(JNIEnv *env, jobject _this) 
{
	jboolean	retVal = 0;

	JFAULT_TOLERANT_SCOPE_WITH_RETURN(env,
	{
		libvlc_exception_t*	exception	= JVLC_initException();
		libvlc_instance_t*	instance	= JVLC_getIntfInstance(env, _this);

	    retVal	= (jboolean)libvlc_audio_get_mute(instance, exception);
	
	    CHECK_EXCEPTION_FREE;
	}, retVal);
	
	return retVal;
}

JNIEXPORT void JNICALL Java_com_googlecode_legendtv_core_Audio_setMuted(JNIEnv *env, jobject _this, jboolean value) 
{
	JFAULT_TOLERANT_SCOPE(env,
	{
	    libvlc_exception_t*	exception	= JVLC_initException();
		libvlc_instance_t*	instance	= JVLC_getIntfInstance(env, _this);
		
		libvlc_audio_set_mute(instance, value, exception);
	  
	    CHECK_EXCEPTION_FREE;
	});
}

JNIEXPORT void JNICALL Java_com_googlecode_legendtv_core_Audio_toggleMute(JNIEnv *env, jobject _this) 
{
	JFAULT_TOLERANT_SCOPE(env,
	{
	    libvlc_exception_t*	exception	= JVLC_initException();
		libvlc_instance_t*	instance	= JVLC_getIntfInstance(env, _this);
		
		libvlc_audio_get_mute(instance, exception);
	    
	    CHECK_EXCEPTION_FREE;
	});
}

JNIEXPORT jint JNICALL Java_com_googlecode_legendtv_core_Audio_getVolume(JNIEnv *env, jobject _this)
{
	jint	retVal = 0;
	
	JFAULT_TOLERANT_SCOPE_WITH_RETURN(env,
	{
		libvlc_exception_t*	exception	= JVLC_initException();
		libvlc_instance_t*	instance	= JVLC_getIntfInstance(env, _this);
		
	    retVal	= libvlc_audio_get_volume(instance, exception);
	
	    CHECK_EXCEPTION_FREE;
	}, retVal);
	
	return retVal;
}

JNIEXPORT void JNICALL Java_com_googlecode_legendtv_core_Audio_setVolume(JNIEnv *env, jobject _this, jint volume)
{
	JFAULT_TOLERANT_SCOPE(env,
	{
	    libvlc_exception_t*	exception	= JVLC_initException();
		libvlc_instance_t*	instance	= JVLC_getIntfInstance(env, _this);
		
		libvlc_audio_set_volume(instance, volume, exception);
	
	    CHECK_EXCEPTION_FREE;
	});
}
